package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dao.AccountTblMapper;
import org.example.entity.AccountTbl;
import org.example.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {



    @Autowired
    private AccountTblMapper accountTblMapper;

    private static Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Override
    public void debit(String userId, int money) {


        logger.info("准备扣减用户：{} 余额，扣减的数目为：{}", userId, money);
//        AccountTbl accountTbl = accountTblMapper.selectOne(new LambdaQueryWrapper<AccountTbl>().eq(AccountTbl::getUserId, userId));
        AccountTbl accountTbl = accountTblMapper.selectOne(userId);
        int idleMoney = accountTbl.getMoney() - money;
        if (idleMoney < 0) {
            throw new RuntimeException("用户余额不足");
        }
        accountTbl.setMoney(idleMoney);
        accountTblMapper.updateById(accountTbl);
        logger.info("扣减用户{}金额成功，剩余金额为{}", userId, money);
    }
}
